package com.zhifei.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhifei.common.utils.Constant;
import com.zhifei.common.utils.DateUtils;
import com.zhifei.common.utils.DispatcherUtils;
import com.zhifei.common.utils.StringUtils;
import com.zhifei.system.domain.EmployeeEntity;
import com.zhifei.system.domain.vo.EmployeeGanttChartItemVo;
import com.zhifei.system.domain.vo.EmployeeGanttChartPageVo;
import com.zhifei.system.domain.vo.EmployeeGanttChartVo;
import com.zhifei.system.domain.vo.EmployeeVo;
import com.zhifei.system.mapper.EmployeeMapper;
import com.zhifei.system.service.EmployeeService;
import com.zhifei.system.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Wrapper;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, EmployeeEntity> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page page = new Page(Long.parseLong((String)params.get(Constant.PAGE)),Long.parseLong((String)params.get(Constant.LIMIT)));
        String customerName = (String) params.get("customerName");
        Integer employeeStatus = DispatcherUtils.parseInteger(params.get("employeeStatus"));
        String userPost = (String) params.get("userPost");
        String userRank = (String) params.get("userRank");
        String admissionStartDate = (String) params.get("admissionStartDate");
        String admissionEndDate = (String) params.get("admissionEndDate");
        String leaveProjectStartDate = (String) params.get("leaveProjectStartDate");
        String leaveProjectEndDate = (String) params.get("leaveProjectEndDate");
        String serviceManager = (String) params.get("serviceManager");
        String recruitHr = (String) params.get("recruitHr");
        Integer leaveProjectStatus = DispatcherUtils.parseInteger(params.get("leaveProjectStatus"));
        String name = (String) params.get("name");
        String orderNo = (String) params.get("orderNo");
        String sortField = (String) params.get("sortField");
        String sortType = (String) params.get("sortType");
        List<EmployeeVo> employeeList = employeeMapper.queryPage(page,customerName,employeeStatus,userPost,userRank,admissionStartDate,admissionEndDate,
                leaveProjectStartDate,leaveProjectEndDate,serviceManager,recruitHr,leaveProjectStatus,name,orderNo,sortField,sortType);
        page.setRecords(employeeList);
        return new PageUtils(page);
    }

    @Override
    public EmployeeGanttChartPageVo pageGanttChart(Map<String, Object> params) {
        EmployeeGanttChartPageVo employeeGanttChartPageVo = new EmployeeGanttChartPageVo();
        Page page = new Page(Long.parseLong((String)params.get(Constant.PAGE)),Long.parseLong((String)params.get(Constant.LIMIT)));
        String customerName = (String) params.get("customerName");
        Integer employeeStatus = DispatcherUtils.parseInteger(params.get("employeeStatus"));
        String userPost = (String) params.get("userPost");
        String userRank = (String) params.get("userRank");
        String admissionStartDate = (String) params.get("admissionStartDate");
        String admissionEndDate = (String) params.get("admissionEndDate");
        String leaveProjectStartDate = (String) params.get("leaveProjectStartDate");
        String leaveProjectEndDate = (String) params.get("leaveProjectEndDate");
        String serviceManager = (String) params.get("serviceManager");
        String recruitHr = (String) params.get("recruitHr");
        Integer leaveProjectStatus = DispatcherUtils.parseInteger(params.get("leaveProjectStatus"));
        String name = (String) params.get("name");
        String orderNo = (String) params.get("orderNo");
        List<EmployeeGanttChartVo> employeeList =  employeeMapper.pageEmployeeGroupByJobNum(page,customerName,employeeStatus,userPost,userRank,admissionStartDate,admissionEndDate,
                 leaveProjectStartDate,leaveProjectEndDate,serviceManager,recruitHr,leaveProjectStatus,name,orderNo);
        List<String> jobNumList = employeeList.stream().map(EmployeeGanttChartVo::getJobNum).collect(Collectors.toList());
        List<EmployeeGanttChartItemVo> employeeGanttChartItemVoList = employeeMapper.listEmployeeGanttChartItem(jobNumList);
        //计算最小入场时间和最大出项时间
        List<String> admissionDateList = employeeGanttChartItemVoList.stream().map(EmployeeGanttChartItemVo::getAdmissionDate).collect(Collectors.toList());
        List<String> leaveProjectDateList = employeeGanttChartItemVoList.stream().map(EmployeeGanttChartItemVo::getLeaveProjectDate).collect(Collectors.toList());
        String minAdmissionDate = showMinDate(admissionDateList);
        String maxLeaveProjectDate = showMaxDate(leaveProjectDateList);
        employeeGanttChartPageVo.setMinAdmissionDate(minAdmissionDate);
        employeeGanttChartPageVo.setMaxLeaveProjectDate(maxLeaveProjectDate);
        for(EmployeeGanttChartVo employeeGanttChartVo : employeeList){
            List<EmployeeGanttChartItemVo> employeeGanttChartItemVos = new ArrayList<>();
            for(EmployeeGanttChartItemVo employeeGanttChartItemVo : employeeGanttChartItemVoList){
                if(employeeGanttChartVo.getJobNum().equals(employeeGanttChartItemVo.getJobNum())){
                    employeeGanttChartItemVos.add(employeeGanttChartItemVo);
                }
            }
            employeeGanttChartVo.setStatusList(employeeGanttChartItemVos);
        }
        page.setRecords(employeeList);
        employeeGanttChartPageVo.setPage(new PageUtils(page));
        return employeeGanttChartPageVo;
    }

    @Override
    public void cacl(String id) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",id);
        List<EmployeeEntity> employeeEntityList = employeeMapper.selectList(wrapper);
        for(EmployeeEntity employeeEntity : employeeEntityList){
            this.caclPrice(employeeEntity);
        }
    }

    @Override
    public void caclPrice(EmployeeEntity employeeEntity){
        BigDecimal quoteMonthPay = employeeEntity.getQuoteMonthPay()==null?BigDecimal.ZERO:employeeEntity.getQuoteMonthPay();//报价月薪
        BigDecimal monthPay = employeeEntity.getMonthPay()==null?BigDecimal.ZERO:employeeEntity.getMonthPay();//薪资
        BigDecimal taxRate = employeeEntity.getTaxRate()==null?BigDecimal.ZERO:employeeEntity.getTaxRate();//税率
        if(employeeEntity.getProjectType()==2 || quoteMonthPay.compareTo(BigDecimal.ZERO)==0
                || monthPay.compareTo(BigDecimal.ZERO)==0 || taxRate.compareTo(BigDecimal.ZERO)==0){
            return;
        }
        Date admissionDate = employeeEntity.getAdmissionDate();//入场时间
//        BigDecimal socialSecurity = new BigDecimal(1126);//社保公积金（默认为1126）
        BigDecimal socialSecurity = employeeEntity.getSocialSecurity();
        employeeEntity.setSocialSecurity(socialSecurity);
        BigDecimal taxAmount = quoteMonthPay.multiply((taxRate.divide(new BigDecimal(100),2, RoundingMode.HALF_UP)));//税额（报价月薪*税率）
        employeeEntity.setTaxAmount(taxAmount);
        BigDecimal grossProfit = quoteMonthPay.subtract(monthPay).subtract(socialSecurity).subtract(taxAmount);//毛利（报价-薪资-社保公积金-税额）
        employeeEntity.setGrossProfit(grossProfit);
        BigDecimal grossProfitRate = grossProfit.divide(quoteMonthPay,2,RoundingMode.HALF_UP);//毛利率（毛利/报价，如有调整实时更新，低于18%显示红色预警）
        employeeEntity.setGrossProfitRate(grossProfitRate);
        Date estimateLeaveDate = DateUtils.addMonths(admissionDate,DispatcherUtils.parseInteger(employeeEntity.getProjectCycle()));;//预计出项时间
        employeeEntity.setEstimateLeaveDate(estimateLeaveDate);
        BigDecimal companyManagementCost = quoteMonthPay.multiply(new BigDecimal(0.1));//公司管理成本（报价*10%）
        employeeEntity.setCompanyManagementCost(companyManagementCost);
        BigDecimal riskReserve = quoteMonthPay.multiply(new BigDecimal(0.05));//风险储备金（报价*5%）
        employeeEntity.setRiskReserve(riskReserve);
        BigDecimal travelExpenses = BigDecimal.ZERO;//差旅费用（初始值为0，后续接口取金蝶中人员差旅成本）
        employeeEntity.setTravelExpenses(travelExpenses);
        BigDecimal marketingSubsidies = quoteMonthPay.multiply(new BigDecimal(0.01));//市场营销补贴（报价*1%）
        employeeEntity.setMarketingSubsidies(marketingSubsidies);
        BigDecimal financialExpenses = quoteMonthPay.multiply(new BigDecimal(0.012));//财务费用（报价*1.2%）
        employeeEntity.setFinancialExpenses(financialExpenses);
        String paymentMethod = employeeEntity.getPaymentMethod();
        String businessSource = employeeEntity.getBusinessSource();
        String userRank = employeeEntity.getUserRank();
        BigDecimal salesCommission = BigDecimal.ZERO;//销售提成（毛利率<=18%为0，毛利率在18到30%为毛利*2%，毛利率在30%以上为毛利*2.5%）
        BigDecimal commissionRate = BigDecimal.ZERO;//提成比例
        if(grossProfitRate.compareTo(new BigDecimal(0.1))<=0){
            commissionRate = BigDecimal.ZERO;
        }else if(grossProfitRate.compareTo(new BigDecimal(0.1))>0 && grossProfitRate.compareTo(new BigDecimal(0.2))<=0){
            commissionRate = new BigDecimal(0.01);
        }else if(grossProfitRate.compareTo(new BigDecimal(0.2))>0 && grossProfitRate.compareTo(new BigDecimal(0.3))<=0){
            commissionRate = new BigDecimal(0.03);
        }else if(grossProfitRate.compareTo(new BigDecimal(0.3))>0){
            commissionRate = new BigDecimal(0.05);
        }
        //鼓励月结回款，月结项目再加0.5%，独立新拓项目增加1%。
        if(grossProfitRate.compareTo(new BigDecimal(0.1))>0){
            if(StringUtils.isNotEmpty(paymentMethod) && paymentMethod.equals("月结")){
                commissionRate = commissionRate.add(new BigDecimal(0.005));
            }
            if(StringUtils.isNotEmpty(businessSource) && businessSource.equals("个人开拓")){
                commissionRate = commissionRate.add(new BigDecimal(0.01));
            }
        }
        salesCommission = grossProfit.multiply(commissionRate);
        BigDecimal recruitCommission = BigDecimal.ZERO;//招聘提成（按照初级250元/人、中级350元/人、高级岗位450元/人；毛利在30%以上增加100元/人）
        if(StringUtils.isNotEmpty(userRank)){
            if(userRank.equals("初级")){
                recruitCommission = new BigDecimal(250);
            }else if(userRank.equals("初中级") || userRank.equals("中级")){
                recruitCommission = new BigDecimal(350);
            }else if(userRank.equals("中高级") || userRank.equals("高级")){
                recruitCommission = new BigDecimal(450);
            }
        }
        if(grossProfitRate.compareTo(new BigDecimal(0.3))>0){
            recruitCommission = recruitCommission.add(new BigDecimal(100));
        }
        employeeEntity.setSalesCommission(salesCommission);//销售提成（毛利率<=18%为0，毛利率在18到30%为毛利*2%，毛利率在30%以上为毛利*2.5%）
        employeeEntity.setRecruitCommission(recruitCommission);
        employeeEntity.setPaymentMethod(paymentMethod);
        employeeEntity.setBusinessSource(businessSource);
//        BigDecimal talentServiceCommission = grossProfit.multiply(new BigDecimal(0.02));//人才服务部提成（毛利*2%）
        BigDecimal talentServiceCommission = BigDecimal.ZERO;
        employeeEntity.setTalentServiceCommission(talentServiceCommission);
        BigDecimal netProfit = grossProfit.subtract(companyManagementCost).subtract(riskReserve).subtract(travelExpenses)
                .subtract(marketingSubsidies).subtract(financialExpenses).subtract(salesCommission).subtract(talentServiceCommission).subtract(recruitCommission);//纯利（毛利-公司管理成本-风险储备金-差旅费用-市场营销补贴-财务费用-销售提成-人才服务部提成）
        employeeEntity.setNetProfit(netProfit);
        BigDecimal netProfitRate = netProfit.divide(quoteMonthPay,2,RoundingMode.HALF_UP);//纯利率（纯利/报价）
        employeeEntity.setNetProfitRate(netProfitRate);
        baseMapper.updateById(employeeEntity);
    }

    @Override
    public void caclPriceBatch(List<EmployeeEntity> employeeEntityList){
        for(EmployeeEntity employeeEntity : employeeEntityList){
            BigDecimal quoteMonthPay = employeeEntity.getQuoteMonthPay()==null?BigDecimal.ZERO:employeeEntity.getQuoteMonthPay();//报价月薪
            BigDecimal monthPay = employeeEntity.getMonthPay()==null?BigDecimal.ZERO:employeeEntity.getMonthPay();//薪资
            BigDecimal taxRate = employeeEntity.getTaxRate()==null?BigDecimal.ZERO:employeeEntity.getTaxRate();//税率
            if(employeeEntity.getProjectType()==2 || quoteMonthPay.compareTo(BigDecimal.ZERO)==0
                    || monthPay.compareTo(BigDecimal.ZERO)==0 || taxRate.compareTo(BigDecimal.ZERO)==0){
                return;
            }
            Date admissionDate = employeeEntity.getAdmissionDate();//入场时间
//        BigDecimal socialSecurity = new BigDecimal(1126);//社保公积金（默认为1126）
            BigDecimal socialSecurity = employeeEntity.getSocialSecurity();
            employeeEntity.setSocialSecurity(socialSecurity);
            BigDecimal taxAmount = quoteMonthPay.multiply((taxRate.divide(new BigDecimal(100),2, RoundingMode.HALF_UP)));//税额（报价月薪*税率）
            employeeEntity.setTaxAmount(taxAmount);
            BigDecimal grossProfit = quoteMonthPay.subtract(monthPay).subtract(socialSecurity).subtract(taxAmount);//毛利（报价-薪资-社保公积金-税额）
            employeeEntity.setGrossProfit(grossProfit);
            BigDecimal grossProfitRate = grossProfit.divide(quoteMonthPay,2,RoundingMode.HALF_UP);//毛利率（毛利/报价，如有调整实时更新，低于18%显示红色预警）
            employeeEntity.setGrossProfitRate(grossProfitRate);
            Date estimateLeaveDate = DateUtils.addMonths(admissionDate,DispatcherUtils.parseInteger(employeeEntity.getProjectCycle()));;//预计出项时间
            employeeEntity.setEstimateLeaveDate(estimateLeaveDate);
            BigDecimal companyManagementCost = quoteMonthPay.multiply(new BigDecimal(0.1));//公司管理成本（报价*10%）
            employeeEntity.setCompanyManagementCost(companyManagementCost);
            BigDecimal riskReserve = quoteMonthPay.multiply(new BigDecimal(0.05));//风险储备金（报价*5%）
            employeeEntity.setRiskReserve(riskReserve);
            BigDecimal travelExpenses = BigDecimal.ZERO;//差旅费用（初始值为0，后续接口取金蝶中人员差旅成本）
            employeeEntity.setTravelExpenses(travelExpenses);
            BigDecimal marketingSubsidies = quoteMonthPay.multiply(new BigDecimal(0.01));//市场营销补贴（报价*1%）
            employeeEntity.setMarketingSubsidies(marketingSubsidies);
            BigDecimal financialExpenses = quoteMonthPay.multiply(new BigDecimal(0.012));//财务费用（报价*1.2%）
            employeeEntity.setFinancialExpenses(financialExpenses);
            String paymentMethod = employeeEntity.getPaymentMethod();
            String businessSource = employeeEntity.getBusinessSource();
            String userRank = employeeEntity.getUserRank();
            BigDecimal salesCommission = BigDecimal.ZERO;//销售提成（毛利率<=18%为0，毛利率在18到30%为毛利*2%，毛利率在30%以上为毛利*2.5%）
            BigDecimal commissionRate = BigDecimal.ZERO;//提成比例
            if(grossProfitRate.compareTo(new BigDecimal(0.1))<=0){
                commissionRate = BigDecimal.ZERO;
            }else if(grossProfitRate.compareTo(new BigDecimal(0.1))>0 && grossProfitRate.compareTo(new BigDecimal(0.2))<=0){
                commissionRate = new BigDecimal(0.01);
            }else if(grossProfitRate.compareTo(new BigDecimal(0.2))>0 && grossProfitRate.compareTo(new BigDecimal(0.3))<=0){
                commissionRate = new BigDecimal(0.03);
            }else if(grossProfitRate.compareTo(new BigDecimal(0.3))>0){
                commissionRate = new BigDecimal(0.05);
            }
            //鼓励月结回款，月结项目再加0.5%，独立新拓项目增加1%。
            if(grossProfitRate.compareTo(new BigDecimal(0.1))>0){
                if(StringUtils.isNotEmpty(paymentMethod) && paymentMethod.equals("月结")){
                    commissionRate = commissionRate.add(new BigDecimal(0.005));
                }
                if(StringUtils.isNotEmpty(businessSource) && businessSource.equals("个人开拓")){
                    commissionRate = commissionRate.add(new BigDecimal(0.01));
                }
            }
            salesCommission = grossProfit.multiply(commissionRate);
            BigDecimal recruitCommission = BigDecimal.ZERO;//招聘提成（按照初级250元/人、中级350元/人、高级岗位450元/人；毛利在30%以上增加100元/人）
            if(StringUtils.isNotEmpty(userRank)){
                if(userRank.equals("初级")){
                    recruitCommission = new BigDecimal(250);
                }else if(userRank.equals("初中级") || userRank.equals("中级")){
                    recruitCommission = new BigDecimal(350);
                }else if(userRank.equals("中高级") || userRank.equals("高级")){
                    recruitCommission = new BigDecimal(450);
                }
            }
            if(grossProfitRate.compareTo(new BigDecimal(0.3))>0){
                recruitCommission = recruitCommission.add(new BigDecimal(100));
            }
            employeeEntity.setSalesCommission(salesCommission);//销售提成（毛利率<=18%为0，毛利率在18到30%为毛利*2%，毛利率在30%以上为毛利*2.5%）
            employeeEntity.setRecruitCommission(recruitCommission);
            employeeEntity.setPaymentMethod(paymentMethod);
            employeeEntity.setBusinessSource(businessSource);
//            BigDecimal talentServiceCommission = grossProfit.multiply(new BigDecimal(0.02));//人才服务部提成（毛利*2%）
            BigDecimal talentServiceCommission = BigDecimal.ZERO;
            employeeEntity.setTalentServiceCommission(talentServiceCommission);
            BigDecimal netProfit = grossProfit.subtract(companyManagementCost).subtract(riskReserve).subtract(travelExpenses)
                    .subtract(marketingSubsidies).subtract(financialExpenses).subtract(salesCommission).subtract(talentServiceCommission).subtract(recruitCommission);//纯利（毛利-公司管理成本-风险储备金-差旅费用-市场营销补贴-财务费用-销售提成-人才服务部提成）
            employeeEntity.setNetProfit(netProfit);
            BigDecimal netProfitRate = netProfit.divide(quoteMonthPay,2,RoundingMode.HALF_UP);//纯利率（纯利/报价）
            employeeEntity.setNetProfitRate(netProfitRate);
            baseMapper.updateById(employeeEntity);
        }

    }

    @Override
    public List<EmployeeEntity> listDistinctEmployee() {
        return employeeMapper.listDistinctEmployee();
    }

    @Override
    public void dismissEmployee(String jobNum,String dismissDate) {
        employeeMapper.dismissEmployee(jobNum,dismissDate);
    }

    @Override
    public void updateEmployee(String jobNum, String userPost, String userRank, BigDecimal monthPay, String recruitHr) {
        employeeMapper.updateEmployee(jobNum,userPost,userRank,monthPay,recruitHr);
    }

    @Override
    public void updatePost(String jobNum, String updatePostDate) {
        //1.查询上一个项目信息
        EmployeeEntity PreProjectEmployeeEntity = employeeMapper.getPreProjectEmployee(jobNum);
        //2.对上一个项目进行在职出项
        employeeMapper.updatePost(PreProjectEmployeeEntity.getId(),updatePostDate);
        //3.复制一条这个人的信息为在职在项目
        EmployeeEntity employeeEntity = new EmployeeEntity();
        employeeEntity.setProjectType(2);
        employeeEntity.setJobNum(PreProjectEmployeeEntity.getJobNum());
        employeeEntity.setFullName(PreProjectEmployeeEntity.getFullName());
        employeeEntity.setUserPost(PreProjectEmployeeEntity.getUserPost());
        employeeEntity.setUserRank(PreProjectEmployeeEntity.getUserRank());
        employeeEntity.setAdmissionDate(DateUtils.parseDate(updatePostDate));
        employeeEntity.setMonthPay(PreProjectEmployeeEntity.getMonthPay());
        employeeEntity.setSocialSecurity(PreProjectEmployeeEntity.getSocialSecurity());
        employeeEntity.setRecruitHr(PreProjectEmployeeEntity.getRecruitHr());
        employeeEntity.setEmployeeStatus(1);
        this.save(employeeEntity);
    }

    @Override
    public EmployeeEntity getPreProjectEmployee(String jobNum) {
        return employeeMapper.getPreProjectEmployee(jobNum);
    }

    @Override
    public void caclPriceByTravelExpenses(String jobNum, BigDecimal travelExpenses) {
        EmployeeEntity employeeEntity = employeeMapper.getEmployee(jobNum);
        if(employeeEntity==null){
            return;
        }
        employeeEntity.setTravelExpenses(travelExpenses);
        BigDecimal companyManagementCost = employeeEntity.getCompanyManagementCost();
        BigDecimal riskReserve = employeeEntity.getRiskReserve();
        BigDecimal marketingSubsidies = employeeEntity.getMarketingSubsidies();
        BigDecimal financialExpenses = employeeEntity.getFinancialExpenses();
        BigDecimal grossProfit = employeeEntity.getGrossProfit();
        BigDecimal salesCommission = employeeEntity.getSalesCommission();
        BigDecimal talentServiceCommission = employeeEntity.getTalentServiceCommission();
        employeeEntity.setTalentServiceCommission(talentServiceCommission);
        BigDecimal netProfit = grossProfit.subtract(companyManagementCost).subtract(riskReserve).subtract(travelExpenses)
                .subtract(marketingSubsidies).subtract(financialExpenses).subtract(salesCommission).subtract(talentServiceCommission);//纯利（毛利-公司管理成本-风险储备金-差旅费用-市场营销补贴-财务费用-销售提成-人才服务部提成）
        employeeEntity.setNetProfit(netProfit);
        BigDecimal netProfitRate = netProfit.divide(employeeEntity.getQuoteMonthPay(),2,RoundingMode.HALF_UP);//纯利率（纯利/报价）
        employeeEntity.setNetProfitRate(netProfitRate);
        baseMapper.updateById(employeeEntity);
    }

    private static String showMaxDate(List<String> dateArray) {
        if(dateArray==null || dateArray.size()==0){
            return null;
        }
        Map<String, Integer> dateMap = new TreeMap<String, Integer>();
        int i, arrayLen;
        arrayLen = dateArray.size();
        for(i = 0; i < arrayLen; i++){
            String dateKey = dateArray.get(i);
            if(dateMap.containsKey(dateKey)){
                int value = dateMap.get(dateKey) + 1;
                dateMap.put(dateKey, value);
            }else{
                dateMap.put(dateKey, 1);
            }
        }
        Set<String> keySet = dateMap.keySet();
        String []sorttedArray = new String[keySet.size()];
        Iterator<String> iter = keySet.iterator();
        int index = 0;
        while (iter.hasNext()) {
            String key = iter.next();
            sorttedArray[index++] = key;
        }
        return sorttedArray[sorttedArray.length - 1];
    }

    private static String showMinDate(List<String> dateArray) {
        if(dateArray==null || dateArray.size()==0){
            return null;
        }
        Map<String, Integer> dateMap = new TreeMap<String, Integer>();
        int i, arrayLen;
        arrayLen = dateArray.size();
        for(i = 0; i < arrayLen; i++){
            String dateKey = dateArray.get(i);
            if(dateMap.containsKey(dateKey)){
                int value = dateMap.get(dateKey) + 1;
                dateMap.put(dateKey, value);
            }else{
                dateMap.put(dateKey, 1);
            }
        }
        Set<String> keySet = dateMap.keySet();
        String []sorttedArray = new String[keySet.size()];
        Iterator<String> iter = keySet.iterator();
        int index = 0;
        while (iter.hasNext()) {
            String key = iter.next();
            sorttedArray[index++] = key;
        }
        return sorttedArray[0];
    }

    public static void main(String[] args) {
        String [] dateArray = {"2013-04-01","2013-04-08","2012-04-28","2013-04-08", "2013-11-11"};
        String a = showMaxDate(Arrays.asList(dateArray));
        System.out.println("a"+a);
        String b = showMinDate(Arrays.asList(dateArray));
        System.out.println("b"+b);
    }

}
